//
//  StatusListViewModel.swift
//  SinaWeibo
//
//  Created by mac on 15/12/29.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

class StatusListViewModel: NSObject {

    class  func loadHomePageData(since_id:Int64,max_id:Int64, finished:(statues:[Status]?) ->() ) {
    //实现网络请求
  
    //get请求
    let urlString = "2/statuses/home_timeline.json"
    //判断token是否为空
    guard let token = UserAccountViewModel().token else {
    print("token为空")
    SVProgressHUD.showInfoWithStatus("请重新登陆")
    return
    }
    
    let parameters = ["access_token" : token]
    NetworkTools.sharedTools.requestJSONDict(.GET, urlString: urlString, parameters: parameters) { (dict, error) -> () in
        if error != nil {
            //执行失败的回调的
            SVProgressHUD.showInfoWithStatus("您的网络正在睡觉,请稍后...")
            finished(statues: nil)
            return
        }
        
        //请求成功
        if let array = dict!["statuses"] as? [[String : AnyObject]] {
            //                    print(array)
            //TODO: 遍历数组中 所有的字典 做字典转模型的操作
            var list = [Status]()
            for item in array {
                let s = Status(dict: item)
                list.append(s)
            }
            
            //执行成功的回调
            finished(statues: list)
        }
    }
    }
}
